import { getStorage } from '@/api/file/netFile.js'

const state = {
  showUploadMask: false,
  storageValue: 0, //  文件已占用的存储空间大小
  totalStorageValue: 0,
  // 表格中显示的列
  selectedColumnList: localStorage.getItem('file_selected_column_list'),
  // 文件展示模式 0 列表模式 | 1 网格模式 | 2 时间线模式
  fileModel: localStorage.getItem('show_file_model'),
  // 网格模式 & 时间线模式下 图标大小 单位px
  gridSize: localStorage.getItem('show_grid_size') ? Number(localStorage.getItem('show_grid_size')) : 80,
  // 批量模式下：被选中的文件列表
  selectedFiles: [],
  // 是否批量操作：true - 批量，false - 单文件
  isBatchOperation: false,
  fileScreenWidth: document.body.clientWidth //  屏幕宽度
}

const mutations = {
  SHOW_UPLOAD_MASK: (state, collapse) => {
    state.showUploadMask = collapse
  },
  SET_STORAGE_VALUE: (state, data) => {
    state.storageValue = data
  },
  SET_TOTAL_STORAGE_VALUE: (state, data) => {
    state.totalStorageValue = data
  },
  CHANGE_SELECTED_COLUMN_LIST: (state, data) => {
    localStorage.setItem('file_selected_column_list', data.toString())
    state.selectedColumnList = data.toString()
  },
  CHANGE_FILE_MODEL: (state, data) => {
    localStorage.setItem('show_file_model', data)
    state.fileModel = data
  },
  CHANGE_GRID_SIZE: (state, data) => {
    localStorage.setItem('show_grid_size', data)
    state.gridSize = data
  },
  CHANGE_SELECTED_FILES: (state, data) => {
    state.selectedFiles = data
  },
  CHANGE_IS_BATCH_OPERATION: (state, data) => {
    state.isBatchOperation = data
  },
  CHANGE_SCREEN_WIDTH: (state, data) => {
    state.fileScreenWidth = data
  }
}

const actions = {
  showUploadMask({ commit }, payload) {
    return new Promise(resolve => {
      commit('SHOW_UPLOAD_MASK', payload)
      resolve()
    })
  },
  showStorage(context) {
    return getStorage().then((res) => {
      if (res) {
        context.commit('SET_STORAGE_VALUE', res ? (res.storageSize === null ? 0 : Number(res.storageSize)) : 0)
        context.commit('SET_TOTAL_STORAGE_VALUE', res ? (res.totalStorageSize === null ? 0 : Number(res.totalStorageSize)) : 0)
      }
    })
  },
  changeSelectedColumnList({ commit }, data) {
    return new Promise(resolve => {
      commit('CHANGE_SELECTED_COLUMN_LIST', data)
      resolve()
    })
  },
  changeFileModel({ commit }, data) {
    return new Promise(resolve => {
      commit('CHANGE_FILE_MODEL', data)
      resolve()
    })
  },
  changeGridSize({ commit }, data) {
    return new Promise(resolve => {
      commit('CHANGE_GRID_SIZE', data)
      resolve()
    })
  },
  changeSelectedFiles({ commit }, data) {
    return new Promise(resolve => {
      commit('CHANGE_SELECTED_FILES', data)
      resolve()
    })
  },
  changeIsBatchOperation({ commit }, data) {
    return new Promise(resolve => {
      commit('CHANGE_IS_BATCH_OPERATION', data)
      resolve()
    })
  },
  changeScreenWidth({ commit }, data) {
    return new Promise(resolve => {
      commit('CHANGE_SCREEN_WIDTH', data)
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
